﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Wicresoft.Framework.Applications;

namespace System.Web.BizCode
{
    public static class WSAFConfig
    {
        // Fields
        private static Application _application = null;
        public const string RoleIDsCookieKey = "WSAF.RoleIDs";
        public const string UserIDCookieKey = "WSAF.UserID";
        public const string UserIDSessionKey = "AuthenticationContext_CurrentUser";

        // Properties
        public static Application Application
        {
            get
            {
                if (_application == null)
                {
                    _application = ApplicationsPublicHelper.GetApplicationInfo(ApplicationID);
                    if (_application == null)
                    {
                        throw new ApplicationException("系统中不存在对应的应用，应用ID：" + ApplicationID);
                    }
                }
                return _application;
            }
        }

        public static Guid ApplicationID
        {
            get
            {
                Guid guid;
                try
                {
                    string g = ConfigurationManager.AppSettings["WSAF.ApplicationID"];
                    guid = new Guid(g);
                }
                catch (Exception exception)
                {
                    throw new ApplicationException("获取应用ID出错，请确认配置项中名称为WSAF.ApplicationID的配置项", exception);
                }
                return guid;
            }
        }

        public static string ApplicationsServiceUrl
        {
            get
            {
                return string.Format("{0}ApplicationsService.asmx", FrameworkWebServiceUrl);
            }
        }

        public static string AuthenticationWebUrl
        {
            get
            {
                string str = ConfigurationManager.AppSettings["WSAF.AuthenticationWebUrl"];
                if ((str == null) || (str.Trim().Length == 0))
                {
                    return string.Format("{0}Authentication.Web/", WSAFServerRootUrl);
                }
                if (!(str.EndsWith(@"\") || str.EndsWith("/")))
                {
                    str = str + "/";
                }
                return str;
            }
        }

        public static string FrameworkWebServiceUrl
        {
            get
            {
                string str = ConfigurationManager.AppSettings["WSAF.FrameworkWebServieUrl"];
                if ((str == null) || (str.Trim().Length == 0))
                {
                    str = string.Format("{0}Framework.WebService/", WSAFServerRootUrl);
                }
                else if (!(str.EndsWith(@"\") || str.EndsWith("/")))
                {
                    str = str + "/";
                }
                if (str.StartsWith("/"))
                {
                    str = "http://localhost" + str;
                }
                return str;
            }
        }

        public static string HttpHandlersDirectory
        {
            get
            {
                return string.Format("{0}HttpHandlers/", FrameworkWebServiceUrl);
            }
        }

        public static string NotAuthorizedPageUrl
        {
            get
            {
                return string.Format("{0}NotAuthorized.aspx", AuthenticationWebUrl);
            }
        }

        public static string OrganizationServiceUrl
        {
            get
            {
                return string.Format("{0}OrganizationService.asmx", FrameworkWebServiceUrl);
            }
        }

        public static string PasswordHashCSP
        {
            get
            {
                return string.Empty;
            }
        }

        public static Guid RootDepartmentID
        {
            get
            {
                Guid guid;
                string g = ConfigurationManager.AppSettings["WSAF.Public.RootDepartmentID"];
                try
                {
                    guid = new Guid(g);
                }
                catch (Exception exception)
                {
                    throw new ApplicationException("获取根组织ID出错，请确认配置项中名称为WSAF.Public.RootDepartmentID的配置项", exception);
                }
                return guid;
            }
        }

        public static string ServerSignInUrl
        {
            get
            {
                return string.Format("{0}SignIn.aspx", AuthenticationWebUrl);
            }
        }

        public static string ServerSignOutUrl
        {
            get
            {
                return string.Format("{0}SignOut.aspx", AuthenticationWebUrl);
            }
        }

        public static bool UseSessionCookie
        {
            get
            {
                string str = ConfigurationManager.AppSettings["WSAF.Public.UseSessionCookie"];
                return ((str == null) || (str.Trim().ToLower() == "true"));
            }
        }

        public static string WSAFServerRootUrl
        {
            get
            {
                string str = ConfigurationManager.AppSettings["WSAF.Server"];
                if ((str == null) || (str.Trim() == string.Empty))
                {
                    return "/";
                }
                if (!(str.EndsWith(@"\") || str.EndsWith("/")))
                {
                    str = str + "/";
                }
                return str;
            }
        }
    }


}